package com.ly.lw.styledo.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.MainDetailAdapter;
import com.ly.lw.styledo.bean.Home;
import com.ly.lw.styledo.bean.PlanList;
import com.ly.lw.styledo.bean.ResultJson;
import com.ly.lw.styledo.config.UrlConstants;
import com.ly.lw.styledo.utils.FileUtils;
import com.ly.lw.styledo.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * Android 仿美团网,大众点评购买框悬浮效果之修改版
 分类： Android 高手进阶
 */
public class MainDetailScro extends FragmentActivity implements AdapterView.OnItemClickListener,View.OnClickListener{

    private MyScrollView mScrollView;
    private ImageView mImageView;
    private LinearLayout mFlowLayout;
    private LinearLayout scroll_lineView;

    //private StaggeredGridView staggeredGridView;
    private ListViewForScrollView listViewForScrollView;
    private BitmapUtils bitmapUtils;
    private MainDetailAdapter mainDetailAdapter;
    //private ArrayList<Normal> listNormals = new ArrayList<>();
    private ArrayList<PlanList> planLists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_detail_scroll);

        initBitmap();
        initView();
        loadData();


    }

    private void initBitmap() {
        bitmapUtils = new BitmapUtils(this, FileUtils.getImageCache());
        //bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_default_item);
        // bitmapUtils.configDefaultLoadingImage(R.color.white);

        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        bitmapUtils.configDefaultImageLoadAnimation(animation);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型

        bitmapUtils.configMemoryCacheEnabled(true);
        bitmapUtils.configDiskCacheEnabled(true);
    }


    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConstants.HOME_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获取服务器返回的json数据
                String result = responseInfo.result;
                Home home = JSONObject.parseObject(result, Home.class);
                if (home != null) {

                    ResultJson resultJson = home.getResult();

                    if (resultJson != null) {
                        List<PlanList> plan_list = resultJson.getPlan_list();
                        if (plan_list != null && plan_list.size() > 0) {
                            planLists.addAll(plan_list);
                            mainDetailAdapter.notifyDataSetChanged();
                        }


                    }


                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
                s = s + 1;

            }
        });
    }




    private void initView() {
        listViewForScrollView = (ListViewForScrollView) findViewById(R.id.list_view);
        mScrollView = (MyScrollView) findViewById(R.id.scroll_view);
        mImageView = (ImageView) findViewById(R.id.image_view);
        mFlowLayout = (LinearLayout) findViewById(R.id.flow_llay);
        scroll_lineView = (LinearLayout) findViewById(R.id.scroll_lineView);
       // ListView listview = (ListView) findViewById(R.id.list_view);
        mainDetailAdapter = new MainDetailAdapter(planLists, this, bitmapUtils);


        listViewForScrollView.setAdapter(mainDetailAdapter);

        listViewForScrollView.setFocusable(false);
        //listview.setOnItemClickListener(this);

        setListViewHeightBasedOnChildren(listViewForScrollView);
        //监听浮动view的滚动状态
        //将 ImageView 设置View
        mScrollView.listenerFlowViewScrollState(mImageView, mFlowLayout);
        //将ScrollView滚动到起始位置
        mScrollView.scrollTo(0, 0);
        //添加 浮动控件的 点击事件
        mFlowLayout.setOnClickListener(this);

    }



    private ArrayList<String> getData() {
        ArrayList<String> data = new ArrayList<String>();
        for (int i = 1; i < 30; i++) {
            data.add("菜单" + i);
        }
        return data;
    }

    /**
     * 用于解决ScrollView嵌套listview时，出现listview只能显示一行的问题
     *
     *
     * 原理是通过 计算 listView子项的个数 来设置listView的 高度
     * @param
     */
    public void setListViewHeightBasedOnChildren(ListViewForScrollView listViewForScrollView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listViewForScrollView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listViewForScrollView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listViewForScrollView.getLayoutParams();
        params.height = totalHeight + (listViewForScrollView.getDividerHeight() * (listAdapter.getCount() - 1)) ;
        // listView.getDividerHeight()获取子项间分隔符占用的高度   + (staggeredGridView. * (listAdapter.getCount() - 1))
        // params.height最后得到整个ListView完整显示需要的高度
        listViewForScrollView.setLayoutParams(params);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // Toast.makeText(MainActivity.this, "菜单" + (position + 1), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        //将ScrollView滚动到起始位置
        mScrollView.scrollTo(0, 0);
    }


}

